# -*- coding:utf-8 -*-
"""Created By Corvo"""

import math

from ..common.base import CombineEntityBaseNew
from ...mdk.common.system.base import *
from ...mdk.server.entity import *


class MantisScorpion(CombineEntityBaseNew):
    """混世巨斧刺比兽"""

    def ActiveAttack(self):
        super(MantisScorpion, self).ActiveAttack()
        self.SetPlayMusic("beast.mantis_scorpion_attack")
        self.skill_module.CastSkill(self.id, "beast_simple_attack", {
            "attack_target": self.GetAttackTarget(),
            "damage": self.attack,
            "delay": 0.6
        })
        self.game_comp.AddTimer(1.0, self.OnSkillFinished)

    def ActiveSkill1(self):
        super(MantisScorpion, self).ActiveSkill1()
        self.SetPlayMusic("beast.mantis_scorpion_sk1")
        self.skill_module.CastSkill(self.id, "mantis_scorpion_sk1", {
            "attack_target": self.GetAttackTarget(),
            "position_offset": [0, self.GetCollisionBoxSize()[1] * 0.5, 4]
        })
        self.game_comp.AddTimer(1.5, self.OnSkillFinished)

    def ActiveSkill2(self):
        super(MantisScorpion, self).ActiveSkill2()
        self.SetPlayMusic("beast.mantis_scorpion_sk2")
        self.skill_module.CastSkill(self.id, "mantis_scorpion_sk2", {
            "attack_target": self.GetAttackTarget(),
        })
        self.game_comp.AddTimer(1.5, self.OnSkillFinished)


class MantisVelociraptor(CombineEntityBaseNew):
    """巨斧速猛兽"""

    def ActiveAttack(self):
        super(MantisVelociraptor, self).ActiveAttack()
        self.SetPlayMusic("beast.mantis_velociraptor_attack")
        self.skill_module.CastSkill(self.id, "beast_simple_attack", {
            "attack_target": self.GetAttackTarget(),
            "damage": self.attack,
            "delay": 0.6
        })
        self.game_comp.AddTimer(1.0, self.OnSkillFinished)

    def ActiveSkill1(self):
        super(MantisVelociraptor, self).ActiveSkill1()
        self.SetPlayMusic("beast.mantis_velociraptor_sk1")
        self.skill_module.CastSkill(self.id, "mantis_velociraptor_sk1", {
            "attack_target": self.GetAttackTarget(),
            "owner_id": self.GetTameOwner()
        })
        self.game_comp.AddTimer(1.75, self.OnSkillFinished)

    def ActiveSkill2(self):
        super(MantisVelociraptor, self).ActiveSkill2()
        self.skill_module.CastSkill(self.id, "mantis_velociraptor_sk2", {
            "attack_target": self.GetAttackTarget(),
            "position_offset": [0, self.GetCollisionBoxSize()[1] * 0.5, 4]
        })
        self.game_comp.AddTimer(1.25, self.OnSkillFinished)


class HornetTiger(CombineEntityBaseNew):
    """刺皇怪纹兽"""

    def __init__(self, entityId):
        super(HornetTiger, self).__init__(entityId)
        self.on_fly = False

    def ActiveAttack(self):
        super(HornetTiger, self).ActiveAttack()
        self.SetPlayMusic("beast.hornet_tiger_attack")
        self.skill_module.CastSkill(self.id, "beast_simple_attack", {
            "attack_target": self.GetAttackTarget(),
            "damage": self.attack,
            "delay": 1.2
        })
        self.game_comp.AddTimer(2.0, self.OnSkillFinished)

    def ActiveSkill1(self):
        super(HornetTiger, self).ActiveSkill1()
        self.SetPlayMusic("beast.hornet_tiger_sk1")
        self.skill_module.CastSkill(self.id, "hornet_tiger_sk1", {
            "attack_target": self.GetAttackTarget(),
        })
        self.game_comp.AddTimer(2.0, self.OnSkillFinished)

    def ActiveSkill2(self):
        super(HornetTiger, self).ActiveSkill2()
        self.SetPlayMusic("beast.hornet_tiger_sk2")
        self.skill_module.CastSkill(self.id, "hornet_tiger_sk2", {
            "attack_target": self.GetAttackTarget(),
        })
        self.game_comp.AddTimer(4.0, self.OnSkillFinished)

    def OnScriptTickServer(self):
        super(HornetTiger, self).OnScriptTickServer()
        if not self.FlyLoop() and self.on_fly:
            self.on_fly = False
            self.StopFly()

    # -----------------------------------------------------------------------------------

    def FlyLoop(self):
        playerId = self.GetTameOwner()
        if not playerId or PlayerEntity(playerId).GetOnRideEntity() != self.id:
            return False
        self.on_fly = True
        face = RawEntity.GetDrt(playerId)

        p = math.atan2(face[1], face[0])
        p2 = math.degrees(p)
        p3 = math.atan2(face[1], face[2])
        p4 = math.degrees(p3)

        if p2 > 15 or p4 > 20:
            self.AddEffect(GameEffect.LEVITATION, 5, 1, False)
            self.AddEffect(GameEffect.MOVEMENT_SPEED, 2, 1, False)
            self.SetGravity(0.00001)
        elif -30 <= p2 <= 15 or -30 <= p4 <= 15:
            self.DelEffect(GameEffect.LEVITATION)
            self.SetGravity(0.00001)
        else:
            self.DelEffect(GameEffect.LEVITATION)
            self.AddEffect(GameEffect.SLOW_FALLING, 4, 1, False)
            self.SetGravity(0.02)
        return True

    def StopFly(self):
        self.DelEffect(GameEffect.LEVITATION)
        self.SetGravity(0)
        playerId = self.GetTameOwner()
        if playerId:
            RawEntity.AddEffect(playerId, GameEffect.SLOW_FALLING, 4, 1, False)


class VelociraptorMantis(CombineEntityBaseNew):
    """速猛巨斧兽"""

    def ActiveAttack(self):
        super(VelociraptorMantis, self).ActiveAttack()
        self.skill_module.CastSkill(self.id, "velociraptor_mantis_attack", {
            "projectile_offset": [0, self.GetCollisionBoxSize()[1] * 0.6, 5],
            "attack_target": self.GetAttackTarget(),
            "damage": self.attack
        })
        self.game_comp.AddTimer(2.0, self.OnSkillFinished)

    def ActiveSkill1(self):
        super(VelociraptorMantis, self).ActiveSkill1()
        self.SetPlayMusic("beast.velociraptor_mantis_sk1")
        self.skill_module.CastSkill(self.id, "velociraptor_mantis_sk1", {
            "attack_target": self.GetAttackTarget(),
        })
        self.game_comp.AddTimer(2.0, self.OnSkillFinished)

    def ActiveSkill2(self):
        super(VelociraptorMantis, self).ActiveSkill2()
        self.SetPlayMusic("beast.velociraptor_mantis_sk2")
        self.skill_module.CastSkill(self.id, "velociraptor_mantis_sk2", {
            "attack_target": self.GetAttackTarget(),
        })
        self.game_comp.AddTimer(2.0, self.OnSkillFinished)


class BearSloth(CombineEntityBaseNew):
    """混世威烈睡爪兽"""

    def ActiveAttack(self):
        super(BearSloth, self).ActiveAttack()
        self.SetPlayMusic("beast.bear_sloth_attack")
        self.skill_module.CastSkill(self.id, "beast_simple_attack", {
            "attack_target": self.GetAttackTarget(),
            "damage": self.attack,
            "delay": 0.7
        })
        self.game_comp.AddTimer(1.4333, self.OnSkillFinished)

    def ActiveSkill1(self):
        super(BearSloth, self).ActiveSkill1()
        self.SetPlayMusic("beast.bear_sloth_sk1")
        self.skill_module.CastSkill(self.id, "bear_sloth_sk1", {
            "attack_target": self.GetAttackTarget()
        })
        self.game_comp.AddTimer(2.9167, self.OnSkillFinished)

    def ActiveSkill2(self):
        super(BearSloth, self).ActiveSkill2()
        self.SetPlayMusic("beast.bear_sloth_sk2")
        self.skill_module.CastSkill(self.id, "bear_sloth_sk2", {
            "attack_target": self.GetAttackTarget()
        })
        self.game_comp.AddTimer(1.4167, self.OnSkillFinished)


class VelociraptorSloth(CombineEntityBaseNew):
    """速猛睡爪兽"""

    def ActiveAttack(self):
        super(VelociraptorSloth, self).ActiveAttack()
        self.SetPlayMusic("beast.velociraptor_sloth_attack")
        self.skill_module.CastSkill(self.id, "beast_simple_attack", {
            "attack_target": self.GetAttackTarget(),
            "damage": self.attack,
            "delay": 0.65
        })
        self.game_comp.AddTimer(1.0, self.OnSkillFinished)

    def ActiveSkill1(self):
        super(VelociraptorSloth, self).ActiveSkill1()
        self.SetPlayMusic("beast.velociraptor_sloth_sk1")
        self.skill_module.CastSkill(self.id, "velociraptor_sloth_sk1", {
            "attack_target": self.GetAttackTarget(),
            "life_time": 1
        })
        self.game_comp.AddTimer(1.0, self.OnSkillFinished)

    def ActiveSkill2(self):
        super(VelociraptorSloth, self).ActiveSkill2()
        self.SetPlayMusic("beast.velociraptor_sloth_sk2")
        self.skill_module.CastSkill(self.id, "velociraptor_sloth_sk2", {
            "attack_target": self.GetAttackTarget(),
            "offset": [0, 0, 6]
        })
        self.game_comp.AddTimer(1.0, self.OnSkillFinished)


class BearBear(CombineEntityBaseNew):
    """双重混世威烈兽"""

    def ActiveAttack(self):
        super(BearBear, self).ActiveAttack()
        self.SetPlayMusic("beast.bear_bear_attack")
        self.skill_module.CastSkill(self.id, "bear_bear_attack", {
            "attack_target": self.GetAttackTarget(),
            "damage": self.attack,
            "delay": 0
        })
        self.game_comp.AddTimer(2.0, self.OnSkillFinished)

    def ActiveSkill1(self):
        super(BearBear, self).ActiveSkill1()
        self.SetPlayMusic("beast.bear_bear_sk1")
        self.skill_module.CastSkill(self.id, "bear_bear_sk1", {
            "attack_target": self.GetAttackTarget(),
            "offset": [0, 3.5, 4.25]
        })
        self.game_comp.AddTimer(1.33, self.OnSkillFinished)

    def ActiveSkill2(self):
        super(BearBear, self).ActiveSkill2()
        self.SetPlayMusic("beast.bear_bear_sk2")
        self.skill_module.CastSkill(self.id, "bear_bear_sk2", {
            "attack_target": self.GetAttackTarget(),
        })
        self.game_comp.AddTimer(3.46, self.OnSkillFinished)


class CowEagleSpider(CombineEntityBaseNew):
    """铠角翱翔灵丝兽"""

    def ActiveAttack(self):
        super(CowEagleSpider, self).ActiveAttack()
        self.SetPlayMusic("beast.cow_eagle_spider_attack")
        self.skill_module.CastSkill(self.id, "beast_simple_attack", {
            "attack_target": self.GetAttackTarget(),
            "damage": self.attack,
            "delay": 0.55
        })
        self.game_comp.AddTimer(1.0, self.OnSkillFinished)

    def ActiveSkill1(self):
        super(CowEagleSpider, self).ActiveSkill1()
        self.SetPlayMusic("beast.cow_eagle_spider_sk1")
        self.skill_module.CastSkill(self.id, "cow_eagle_spider_sk1", {
            "attack_target": self.GetAttackTarget()
        })
        self.game_comp.AddTimer(2.2, self.OnSkillFinished)

    def ActiveSkill2(self):
        super(CowEagleSpider, self).ActiveSkill2()
        self.SetPlayMusic("beast.cow_eagle_spider_sk2")
        self.skill_module.CastSkill(self.id, "cow_eagle_spider_sk2", {
            "attack_target": self.GetAttackTarget(),
        })
        self.game_comp.AddTimer(1.6, self.OnSkillFinished)

    def ActiveSkill3(self):
        super(CowEagleSpider, self).ActiveSkill3()
        self.skill_module.CastSkill(self.id, "cow_eagle_spider_sk3", {
            "attack_target": self.GetAttackTarget(),
        })
        self.game_comp.AddTimer(1.2, self.OnSkillFinished)

    def TriggerAttack(self):
        if not self.IsSkillOnCd("sk1"):
            self.TriggerCustomEvent("event:active_skill1")
        elif not self.IsSkillOnCd("sk2"):
            self.TriggerCustomEvent("event:active_skill2")
        elif not self.IsSkillOnCd("sk3"):
            self.TriggerCustomEvent("event:active_skill3")
        elif not self.IsSkillOnCd("attack"):
            self.TriggerCustomEvent("event:active_attack")


class VelociraptorTigerHornet(CombineEntityBaseNew):
    """速猛怪纹刺皇兽"""

    def ActiveAttack(self):
        super(VelociraptorTigerHornet, self).ActiveAttack()
        self.SetPlayMusic("beast.velociraptor_tiger_hornet_attack")
        self.skill_module.CastSkill(self.id, "beast_simple_attack", {
            "attack_target": self.GetAttackTarget(),
            "damage": self.attack,
            "delay": 0.55
        })
        self.game_comp.AddTimer(1.0, self.OnSkillFinished)

    def ActiveSkill1(self):
        super(VelociraptorTigerHornet, self).ActiveSkill1()
        self.skill_module.CastSkill(self.id, "velociraptor_tiger_hornet_sk1", {
            "attack_target": self.GetAttackTarget()
        })
        self.game_comp.AddTimer(2.0, self.OnSkillFinished)

    def ActiveSkill2(self):
        super(VelociraptorTigerHornet, self).ActiveSkill2()
        self.SetPlayMusic("beast.velociraptor_tiger_hornet_sk2")
        self.skill_module.CastSkill(self.id, "velociraptor_tiger_hornet_sk2", {
            "attack_target": self.GetAttackTarget(),
        })
        self.game_comp.AddTimer(2.0, self.OnSkillFinished)

    def ActiveSkill3(self):
        super(VelociraptorTigerHornet, self).ActiveSkill3()
        self.SetPlayMusic("beast.velociraptor_tiger_hornet_sk3")
        self.skill_module.CastSkill(self.id, "velociraptor_tiger_hornet_sk3", {
            "attack_target": self.GetAttackTarget(),
        })
        self.game_comp.AddTimer(2.0, self.OnSkillFinished)

    def TriggerAttack(self):
        if not self.IsSkillOnCd("sk1"):
            self.TriggerCustomEvent("event:active_skill1")
        elif not self.IsSkillOnCd("sk2"):
            self.TriggerCustomEvent("event:active_skill2")
        elif not self.IsSkillOnCd("sk3"):
            self.TriggerCustomEvent("event:active_skill3")
        elif not self.IsSkillOnCd("attack"):
            self.TriggerCustomEvent("event:active_attack")
